package com.foreverwin.jzymes.common.util;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

public class CollectionUtil {

    public static <T> void sort( List<Map<String,Object>> list, String field ){
        Collections.sort( list, new Comparator<Map<String, Object>>() {
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                String map1value = (String) o1.get( field );
                String map2value = (String) o2.get( field );
                return map1value.compareTo( map2value );
            }
        });
    }

    public static <T> void sort( List<Map<String,Object>> list, String... fields ){
        Collections.sort( list, new Comparator<Map<String, Object>>() {
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                String map1value = "";
                String map2value = "";
                for( String field : fields ){
                    map1value += (String) o1.get( field );
                    map2value += (String) o2.get( field );
                }

                return map1value.compareTo( map2value );
            }
        });
    }
}
